package L_5_8;


import java.util.Objects;

//Object类是所有类的父类
class Person {
    public int age;
    public String name;

    public Person (int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString () {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }


    //将对象的比较equals方法进行重写，alt+insert 一路向下
    @Override
    public boolean equals (Object o) {
        if (this == o) return true;
        if (o == null || getClass () != o.getClass ()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals (name, person.name);
    }

    @Override
    public int hashCode () {
        return Objects.hash (age, name);
    }
}


public class test01 {
    public static void main (String[] args) {
        Person p1 = new Person (10, "hong");
        Person p2 = new Person (20, "huang");
        Person p3 = new Person (10, "hong");

        System.out.println (p1.equals (p2));
        System.out.println (p1.equals (p3));

        //对哈希表的方法进行重写后，同样的数据，虽然新new了一个对象，但还是同一个地址
        System.out.println (p1.hashCode ());
        System.out.println (p3.hashCode ());
    }
}
